<template>
  <div class="attendance">
    <h2>签到管理</h2>
    
    <el-form :inline="true" class="search-form">
      <el-form-item label="活动">
        <el-select v-model="searchForm.activityId" placeholder="请选择活动">
          <el-option
            v-for="item in activities"
            :key="item.activityId"
            :label="item.title"
            :value="item.activityId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="志愿者姓名">
        <el-input v-model="searchForm.name" placeholder="请输入志愿者姓名" />
      </el-form-item>
      <el-form-item label="签到状态">
        <el-select v-model="searchForm.status" placeholder="请选择状态">
          <el-option label="全部" value="" />
          <el-option label="已签到" value="已签到" />
          <el-option label="未签到" value="未签到" />
          <el-option label="已签退" value="已签退" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="filteredAttendanceList">
      <el-table-column prop="volunteerId" label="志愿者编号" width="100" />
      <el-table-column prop="name" label="姓名" width="120" />
      <el-table-column prop="checkInTime" label="签到时间" width="180" />
      <el-table-column prop="checkOutTime" label="签退时间" width="180" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button 
            v-if="!scope.row.checkInTime"
            type="primary" 
            @click="handleCheckIn(scope.row)"
          >
            签到
          </el-button>
          <el-button 
            v-if="scope.row.checkInTime && !scope.row.checkOutTime"
            type="success" 
            @click="handleCheckOut(scope.row)"
          >
            签退
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getActivityAttendance, updateAttendance } from '@/api/activity'

export default {
  data() {
    return {
      attendanceList: [],
      searchForm: {
        activityId: '',
        name: '',
        status: ''
      }
    }
  },
  created() {
    this.fetchAttendance()
  },
  methods: {
    async fetchAttendance() {
      try {
        const data = await getActivityAttendance(this.$route.params.activityId, this.searchForm)
        this.attendanceList = data
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    async handleCheckIn(record) {
      try {
        await updateAttendance(this.$route.params.activityId, record.volunteerId, {
          status: '已签到',
          checkInTime: new Date().toLocaleString()
        })
        this.$message.success('签到成功')
        this.fetchAttendance()
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    async handleCheckOut(record) {
      try {
        await updateAttendance(this.$route.params.activityId, record.volunteerId, {
          status: '已签退',
          checkOutTime: new Date().toLocaleString()
        })
        this.$message.success('签退成功')
        this.fetchAttendance()
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  }
}
</script>

<style scoped>
.search-form {
  margin-bottom: 20px;
  background-color: #f5f7fa;
  padding: 20px;
  border-radius: 4px;
}
</style> 